<template>
    <view class="w-full h-screen box-border pt-[var(--top-m)] bg-[var(--page-bg-color)] setting-wrap" :style="themeColor()">
        <view class="mb-[var(--top-m)] sidebar-margin card-template !py-[20rpx]">
            <u-cell-group :border="false" class="cell-group">
                <u-cell :title="t('personalSettings')" :is-link="true" url="/app/pages/member/personal"></u-cell>
                <u-cell title="支付设置" :is-link="true" url="/app/pages/paymentSetting/index"></u-cell>
                <!-- <u-cell :title="t('switchLang')" :is-link="true" :value="lang" @click="langSheetShow = true"></u-cell> -->
                <u-cell :title="t('version')" :value="version"></u-cell>
                <!-- #ifdef APP-PLUS -->
                <u-cell title="更新" :is-link="true" value="访问下载页" @click="navtoUpdatePage"></u-cell>
                <!-- #endif -->
            </u-cell-group>
        </view>
        <view class="mb-[var(--top-m)] sidebar-margin card-template !py-[20rpx]">
            <u-cell-group :border="false" class="cell-group">
                <u-cell :title="t('userAgreement')" :is-link="true" url="/app/pages/auth/agreement?key=service"></u-cell>
                <u-cell :title="t('privacyAgreement')" :is-link="true" url="/app/pages/auth/agreement?key=privacy"></u-cell>
            </u-cell-group>
        </view>

        <view class="mb-[var(--top-m)] h-[88rpx] flex-center bg-[#fff] mx-[var(--sidebar-m)] rounded-[var(--rounded-big)] text-[28rpx]" @click="handleDeleteAccount">注销账户</view>
        <view class="h-[88rpx] flex-center bg-[#fff] mx-[var(--sidebar-m)] rounded-[var(--rounded-big)] text-[28rpx]" @click="memberStore.logout(true)">{{ t('logout') }}</view>

        <u-action-sheet :actions="langList" :show="langSheetShow" :closeOnClickOverlay="true"
                        :safeAreaInsetBottom="true"
                        @close="langSheetShow = false" @select="switchLang"></u-action-sheet>
    </view>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import useMemberStore from '@/stores/member'
import useSystemStore from '@/stores/system'
import { t, language } from '@/locale'
import permission from '@/utils/permission'

const memberStore = useMemberStore()
const systemStore = useSystemStore()
const siteInfo: any = computed(() => {
    return systemStore.site;
})
const version = ref(import.meta.env.VITE_APP_VERSION)

/**
 * 支持的语言列表
 */
const langList = reactive<any>({
    'zh-Hans': { name: '简体中文', fontSize: '14', value: 'zh-Hans' },
    'en': { name: 'English', fontSize: '14', value: 'en' }
})
const langSheetShow = ref(false)

// 当前语言
const lang = computed(() => {
    const lang = uni.getLocale()
    return langList[lang].name
})

/**
 * 切换语言
 */
const switchLang = (lang: any) => {
    language.loadAllLocaleMessages('app', lang.value)
}

// 访问下载页
const navtoUpdatePage = () => {
    plus.runtime.openURL('https://sichuan.xinyuejingu.com/download')
}

/**
 * 注销账户
 */
const handleDeleteAccount = () => {
    uni.showModal({
        title: '注销账户',
        content: '请联系客服注销账户：' + (siteInfo.value.phone || '暂无客服电话'),
        showCancel: true,
        confirmText: '拨打客服',
        cancelText: '取消',
        success: (res) => {
            if (res.confirm) {
                if (siteInfo.value.phone) {
                    // #ifdef APP-PLUS
                    permission.permissionCheck('CALL_PHONE').then((res: any) => {
                        if (res === 1) {
                            uni.makePhoneCall({
                                phoneNumber: siteInfo.value.phone
                            })
                        }
                    });
                    // #endif
                    // #ifndef APP-PLUS
                    uni.makePhoneCall({
                        phoneNumber: siteInfo.value.phone
                    })
                    // #endif
                } else {
                    uni.showToast({
                        title: '暂无客服电话',
                        icon: 'none'
                    })
                }
            }
        }
    })
}
</script>

<style lang="scss" scoped>
page {
    background: var(--page-bg-color);
}

:deep(.cell-group), :deep(.u-cell-group) {
    .u-cell {
        .u-cell__body {
            padding: 0;
            height: 90rpx;
            margin-top: 16rpx;
        }

        &:first-of-type .u-cell__body {
            margin-top: 0;
        }

        .u-cell__title-text {
            font-size: 28rpx;
            line-height: 40rpx;
        }

        .u-icon__icon {
            font-size: 24rpx !important;
        }

        .u-cell__value {
            line-height: 1;
            font-size: 28rpx;
        }

        .u-line {
            display: none;
        }
    }
}

:deep(.u-action-sheet) {
    .u-line {
        margin: 0 30rpx !important;
        width: auto !important;
        border-color: #ddd !important;
    }

    .u-action-sheet__cancel {
        padding: 0;

    }

    .u-action-sheet__item-wrap__item__name {
        font-size: 30rpx !important;
    }
}
</style>
<style>
.setting-wrap .u-cell--clickable {
    background-color: transparent !important;
}
</style>
